package com.example.service.impl;

import com.example.mapper.MagazineMapper;
import com.example.pojo.Magazine;
import com.example.service.MagazineService;
import com.example.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MagazineServiceImpl implements MagazineService {
    @Autowired
    private MagazineMapper magazineMapper;

    @Override
    public String clearProblemIssn(String path) {
        List<Magazine> magazineList = ExcelUtils.readExcel(path, Magazine.class);
        if(magazineList==null){
            return "文件不存在";
        }
        for (int i=0;i<magazineList.size();i++) {
            Magazine magazine=magazineList.get(i);
            Integer id = magazineMapper.selectIdByMagazine(magazine);
            if(id!=null){
                magazine.setId(id);
            }
        }
        List<Magazine> magazines1 = new ArrayList<>();
        List<Magazine> magazines2 = new ArrayList<>();
        for (Magazine magazine : magazineList) {
            if(magazine.getIssnDelFlag()==1){
                magazine.setIssn("");
                magazines1.add(magazine);
            }
            if(magazine.getIssnDelFlag()==2){
                magazines2.add(magazine);
            }
        }
        int issnNum1 = magazineMapper.updateIssnByMagazines(magazines1);
        int issnNum2 = magazineMapper.updateIssnByMagazines(magazines2);
        return "删除issn"+issnNum1+"个\r\n"+"更新issn"+issnNum2+"个";
    }

    @Override
    public Integer updateDelFlagByMagazines(String deletePath){
        List<Magazine> magazineList = ExcelUtils.readExcel(deletePath, Magazine.class);
        if(magazineList==null){
            return null;
        }
        for (int i=0;i<magazineList.size();i++) {
            Magazine magazine=magazineList.get(i);
            Integer id = magazineMapper.selectIdByMagazine(magazine);
            if(id!=null){
                magazine.setId(id);
            }
        }
        int num = magazineMapper.updateDelFlagByMagazines(magazineList);
        return num;
    }

    @Override
    public String updateMagazineLabelName(String originalPath) {
        List<Magazine> magazineList = ExcelUtils.readExcel(originalPath, Magazine.class);
        if(magazineList==null){
            return "文件不存在！";
        }
        int count=0;
        for (Magazine magazine : magazineList) {
            List<Magazine> magazineList1 = magazineMapper.selectMagazineListByMagazine(magazine);
            for (int i = 0; i < magazineList1.size(); i++) {
                Magazine magazine1=magazineList1.get(i);
                if(i==0){
                    continue;
                }
                String newLabelName= magazine1.getLabelName().substring(0,magazine1.getLabelName().indexOf("》"))+i+"》";
                magazine1.setLabelName(newLabelName);
                count += magazineMapper.update(magazine1);
            }
        }
        return "成功（更新了"+count+"条）";
    }
}
